<template>
  <editForm
    :formConfig="basicConfig"
    ref="basicForm"
    :formData="formData"
  ></editForm>
</template>

<script>
export default {
  props: {
    allInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
    status: {
      type: Number,
    },
  },
  data() {
    return {
      minTime: null,
      maxTime: null,
      basicConfig: {
        labelWidth: "160px",
        itemArr: [
          {
            label: "批复结果",
            prop: "result",
            type: "select",
            required: true,
            disabled: false,
            span: 8,
            selectOption: [
              { label: "同意", value: 1 },
              { label: "不同意", value: 0 },
            ],
          },
          {
            label: "批准起止时间",
            prop: "approvalTime",
            type: "datetimerange",
            required: true,
            disabled: false,
            span: 8,
            rules: {
              trigger: "blur",
              validator: (rule, value, callback) => {
                this.checkTime(rule, value, callback);
              },
            },
            pickerOptions: {
              disabledDate: (time) => {
                return (
                  time.getTime() < this.minTime || time.getTime() > this.maxTime
                );
              },
            },
            methods: (val) => {
              let times =
                new Date(val[1]).getTime() - new Date(val[0]).getTime();
              let hours = times / (60 * 60 * 1000);
              this.$refs.basicForm.setFormItem("delayTimeLength", hours);
              this.$refs.basicForm.setFormItem("startTime", val[0]);
              this.$refs.basicForm.setFormItem("endTime", val[1]);
            },
          },
          {
            label: "批准工作时长(h)",
            prop: "timeLength",
            type: "text",
            required: true,
            disabled: true,
            span: 8,
          },
          {
            label: "批复人",
            prop: "createByName",
            type: "text",
            required: false,
            disabled: true,
            span: 8,
          },
          {
            label: "批复时间",
            prop: "createTime",
            type: "datetime",
            isHide: false,
            required: false,
            disabled: true,
            span: 8,
            methods: (val) => {},
          },
          {
            label: "批复意见",
            prop: "opinion",
            type: "textarea",
            required: false,
            span: 24,
            rows: 4,
          },
        ],
      },
      formData: {},
    };
  },
  created() {
    if (this.status > 1) {
      this.basicConfig.itemArr.map((i) => (i.disabled = true));
    }
  },
  mounted() {},
  watch: {
    allInfo: {
      handler(val) {
        this.init(val);
      },
      deep: true,
    },
    status: {
      handler(val) {
        if (val > 1) {
          this.basicConfig.itemArr.map((i) => (i.disabled = true));
        }
      },
    },
  },
  methods: {
    //初始化
    init(val) {
      let data = {};
      if (val.applicationCheck) {
        data = JSON.parse(JSON.stringify(val.applicationCheck));
        data.approvalTime = [
          val.applicationCheck.startTime,
          val.applicationCheck.endTime,
        ];
      } else {
        data.approvalTime = [val.startTime, val.endTime];
        data.startTime = val.startTime;
        data.endTime = val.endTime;
      }
      let times =
        new Date(data.approvalTime[1]).getTime() - new Date(data.approvalTime[0]).getTime();
      let hours = times / (60 * 60 * 1000);
      data.timeLength = hours.toFixed(2);
      this.formData = data;
      this.minTime = new Date(
        `${this.allInfo.startTime.split(" ")[0]} 00:00`
      ).getTime();
      this.maxTime = new Date(
        `${this.allInfo.endTime.split(" ")[0]} 24:00`
      ).getTime();
    },
    //校验批复时间
    checkTime(rule, val, callback) {
      let minTime = new Date(this.allInfo.startTime).getTime();
      let maxTime = new Date(this.allInfo.endTime).getTime();
      let startTime = new Date(val[0]).getTime();
      let endTime = new Date(val[0]).getTime();
      let message = `请在${this.allInfo.startTime}-${this.allInfo.endTime}内选择`;
      if (startTime < minTime || endTime > maxTime) {
        //debugger
        return callback(new Error(message));
      } else {
        callback();
      }
    },
    async getApprovalInfo() {
      let formData = await this.$refs.basicForm.getFormData();
      formData.applicationId = this.allInfo.id;
      return formData;
    },
  },
};
</script>

<style lang="less" scoped>
</style>